package cn.fengso.servlet;

import java.io.IOException; 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.fengso.config.Config;
import cn.fengso.service.IMessageHandlerService;
import cn.fengso.service.impl.MessageHandlerServiceImpl;
import cn.fengso.vo.Message;

public class ReceiveMessageServlet extends HttpServlet {
 

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
 
	private IMessageHandlerService ms = new MessageHandlerServiceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

 
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String key=request.getParameter("key");
		if(!isNotBlank(key)||!key.equals(Config.getString("KEY"))){
			return;
		}
		String level=request.getParameter("level");
		String city=request.getParameter("city");
		if(!isNotBlank(level)||!isNotBlank(city)){
			return;
		}
		Message msg=new Message();
		msg.setArea(city);
		msg.setLevel(getInteger(level)); 
		ms.receive(msg);
	}
	
	private boolean isNotBlank(String str){
		if(str==null||str.equals("")){
			return false;
		}
		return true;
	}
	
	private int getInteger(String str){
		int i=0;
		try{
			i=Integer.parseInt(str);
		}catch(Exception e){}
		return i;
	}
 

}
